package com.zy.smarthome.frame.util;

import com.zy.smarthome.common.StrAndHex;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据处理
 * @author Administrator
 */
public class FrameDataDeal {
    /**
     * 用于把数据帧变成ByteBuf下发给网关(16进制传输)
     *
     * @return
     */
    public static ByteBuf dataToByte(String data) {
        // 连接即发送5010请求登录帧
        //netty需要用ByteBuf传输
        ByteBuf bufff = Unpooled.buffer();
        byte[] bytes = StrAndHex.hexString2Bytes(data);
        //对接需要16进制
        bufff.writeBytes(bytes);
        return bufff;
    }

    /**
     * 字符串截取，截取把头2A和数据长度，尾部23和校验位去掉
     *
     * @param frameString
     * @return
     */
    public static String getData(String frameString) {
        String big = "FF";
        String ff = StringUtils.substring(frameString, 2, 4);
        if (big.equals(ff)) {
            return frameString.substring(6, frameString.length() - 4);
        } else {
            return frameString.substring(4, frameString.length() - 4);
        }
    }
}
